package com.iraeta.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.order.GiftOrder;
import com.iraeta.entity.resulttype.GiftOrderRT;
import com.iraeta.entity.resulttype.GiftOrderTs;
import com.iraeta.entity.type.GiftOrderStatus;
import com.iraeta.service.order.IGiftOrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController(value = "GiftOrderShenHeController")
@RequestMapping("gift/ordershenhe")
public class GiftOrderShenHeController  extends BaseController {
    @Resource
    IGiftOrderService giftOrderService;

    @RequestMapping("listgiftshenheorder")
    public String listShenHeGiftOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String giftName = jo.getString("giftName");
        String createusername = jo.getString("createusername");
        this.pageSet(jo);
        List<GiftOrderRT> orderRTList = giftOrderService.listDaiShenHeGiftOrder(orderno, giftName, createusername);
        orderRTList.forEach(x->{
            x.setStatusname(GiftOrderStatus.getStatusName(x.getStatusId()));
        });
        PageInfo<GiftOrderRT> orderRTPageInfo = this.getPageInfo(orderRTList);
        return this.success(orderRTPageInfo);
    }

    @RequestMapping("listshenhetsgiftorder")
    public String listShenHeTSGiftOrder(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String createusername = jo.getString("createusername");
        this.pageSet(jo);
        List<GiftOrderTs> orderTsList = giftOrderService.listDaiShenHeTSGiftOrder(orderno, createusername);
        orderTsList.forEach(x->{
            x.setStatusname(GiftOrderStatus.getStatusName(x.getStatusId()));
        });
        PageInfo<GiftOrderTs> orderTsPageInfo = this.getPageInfo(orderTsList);
        return this.success(orderTsPageInfo);
    }

    @RequestMapping("shenheorder")
    public String shenHeOrder(@RequestBody JSONObject jo){
        int id = jo.getIntValue("id");
        int statusId = jo.getIntValue("statusId");
        if(id == 0 || statusId == 0){
            throw new ServiceException("参数不正确");
        }
        String returnReason = jo.getString("returnReason");
        GiftOrder giftOrder = new GiftOrder();
        giftOrder.setId(id);
        giftOrder.setStatusId(statusId);
        giftOrder.setReturnReason(returnReason);
        giftOrder.setShenheUserid(this.getUserId());
        giftOrder.setShenheTime(new Date());
        giftOrderService.shenHeOrder(giftOrder);
        return this.success();
    }

    @RequestMapping("listshenhehistory")
    public String listShenHeHistory(@RequestBody JSONObject jo){
        String orderno = jo.getString("orderno");
        String createusername = jo.getString("createusername");
        this.pageSet(jo);
        List<GiftOrderRT> orderRTList = giftOrderService.listShenHeHistory(this.getUserId(), orderno, createusername);
        orderRTList.forEach(x->{
            x.setStatusname(GiftOrderStatus.getStatusName(x.getStatusId()));
        });
        PageInfo<GiftOrderRT> orderRTPageInfo = this.getPageInfo(orderRTList);
        return this.success(orderRTPageInfo);
    }
}
